<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2019-1-2
 * Time: 9:43
 */

namespace app\index\controller;


use app\common\model\GoodsModel;
use think\Controller;
use think\Cookie;
use think\Request;
use wxpay\H5Pay;
use wxpay\SaoWxPay;

class Wxpay extends Controller
{
    /**
     * 支付二维码接口
     * @return \think\response\Json
     */
    public function scanCode(){

        if(request()->isAjax()){



            $scan = new SaoWxPay();
            $req = request()->post();

            $goods = new GoodsModel();

            $good_id = Cookie::get('good_id');


            $product = $goods->getOneGoods($good_id);//商品


            if ($req['goods_Num'] > $product['good_repertory']){
                return json(['code'=>400,'data'=>'库存不足']);
            }

            //计算总价
            $totalPrice = $product['good_price'] * $req['goods_Num'];

            $params['describe'] = $req['describe'];   //商品描述
            $params['orderNum'] = $req['orderNum'];    //自定义的订单号
            $params['totalPrice'] = $totalPrice;//intval($data['totalPrice']/10);

            $img = $scan->saoRequest($params);

            return json(['code'=>200,'data'=>$img]);

//            echo "<img src='".$img."'/>";
        }
    }

    /**
     * jsapi接口
     */
    public function h5(){
        $req = request()->post();
        $h5 = new H5Pay();

        $goods = new GoodsModel();

        $good_id = Cookie::get('good_id');

        $row = $goods->getOneGoods($good_id);

        if ($req['num'] > $row['good_repertory']){
            return false;
        }

        $req['totalPrice'] = $row['good_price']*$req['num'];//计算总价

        $res = $h5->wxh5Request($req);
        return $res;
    }

    /**
     * 查询订单
     */
    public function query()
    {
        if ($_POST) {
            $req = Request::instance()->post();
            $appid = 'wxf4b7b2771a83d60b';
            $mch_id = '1530809431';//商户号
            $key = '63bdd8ce57da5f738f561b02945609ae';//商户key
            $notify_url = "http://cdweizhichu.com";//回调地址

            $wxPay = new \wxpay\WxPay($appid,$mch_id,$notify_url,$key);

            $res = $wxPay->orderQuery($req['orderNum']);
            return json($res);
        }
    }
}